/**
 * @author David Young
 */
package csab.game;


import org.newdawn.slick.Input;

import csab.core.BehaviorController;
import csab.core.Sprite;
import csab.game.sprites.PlatformAISprite;
import csab.game.sprites.PlayerSprite;

/**
 * This controller controls enemies that perform melee attacks.
 * @author David Young
 */
public class MeleeAIController implements BehaviorController {
	private PlatformAISprite owned;
	public MeleeAIController(Sprite sprite)
	{
		owned = (PlatformAISprite)sprite;
		owned.setMovingLAndR(true);
	}

	/* (non-Javadoc)
	 * @see csab.core.BehaviorController#doBehavior(int, java.util.List, org.newdawn.slick.Input)
	 */
	@Override
	public void doBehavior(int delta, Sprite target, Input i) {
		if(!owned.isDead())
		{
			if(target instanceof PlayerSprite)
			{
				PlayerSprite player = (PlayerSprite)target;
		
				int centerx = (int) (owned.getX() + owned.getWidth() / 2);
				int centery = (int) (owned.getY() + owned.getHeight() / 2);
				int tcx = (int) (player.getX() + player.getWidth() / 2);
				int tcy = (int) (player.getY() + player.getWidth() / 2);
				int difx = tcx - centerx;
				int dify = tcy - centery;
				int distance = (int)Math.sqrt(difx*difx + dify*dify);
				if(distance < 32)
				{
					owned.idle();
					return;
				}
			}
			if(owned.getDirection() == PlatformAISprite.Direction.LEFT)
			{
				owned.moveLeft();
			}
			else if(owned.getDirection() == PlatformAISprite.Direction.RIGHT)
			{
				owned.moveRight();
			}
			else
				owned.idle();
		}
		else
			owned.stopMoving();
	}

	/* (non-Javadoc)
	 * @see csab.core.BehaviorController#getOwnedSprite()
	 */
	@Override
	public Sprite getOwnedSprite() {
		return owned;
	}

}
